package com.aiphome;

import com.aiphome.model.Role;
import com.aiphome.model.ERole;
import com.aiphome.model.User;
import com.aiphome.model.Product;
import com.aiphome.repository.RoleRepository;
import com.aiphome.repository.UserRepository;
import com.aiphome.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.PostConstruct;
import java.util.HashSet;
import java.util.Set;

@SpringBootApplication
public class AipHomeApplication {

    @Autowired
    private RoleRepository roleRepository;
    
    @Autowired
    private UserRepository userRepository;
    
    @Autowired
    private ProductRepository productRepository;
    
    @Autowired
    private org.springframework.security.crypto.password.PasswordEncoder passwordEncoder;

    public static void main(String[] args) {
        SpringApplication.run(AipHomeApplication.class, args);
    }
    
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/api/**").allowedOrigins("*").allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS");
            }
        };
    }
    
    @PostConstruct
    public void initData() {
        // 初始化角色
        if (roleRepository.count() == 0) {
            Role adminRole = new Role();
            adminRole.setName(ERole.ROLE_ADMIN);
            roleRepository.save(adminRole);
            
            Role userRole = new Role();
            userRole.setName(ERole.ROLE_USER);
            roleRepository.save(userRole);
        }
        
        // 创建默认管理员账户
        if (!userRepository.existsByUsername("admin")) {
            User admin = new User();
            admin.setUsername("admin");
            admin.setEmail("admin@example.com");
            admin.setPassword(passwordEncoder.encode("admin123"));
            admin.setFullName("System Administrator");
            
            Set<Role> roles = new HashSet<>();
            roles.add(roleRepository.findByName(ERole.ROLE_ADMIN).orElseThrow(() -> new RuntimeException("Admin role not found")));
            roles.add(roleRepository.findByName(ERole.ROLE_USER).orElseThrow(() -> new RuntimeException("User role not found")));
            admin.setRoles(roles);
            
            userRepository.save(admin);
        }
        
        // 创建一些示例产品数据
        if (productRepository.count() == 0) {
            Product product1 = new Product();
            product1.setName("智能插座");
            product1.setDescription("WiFi控制的智能插座，支持远程开关和定时功能");
            product1.setPrice(89.99);
            product1.setStock(100);
            productRepository.save(product1);
            
            Product product2 = new Product();
            product2.setName("智能灯泡");
            product2.setDescription("可调色温智能LED灯泡，支持语音控制");
            product2.setPrice(59.99);
            product2.setStock(200);
            productRepository.save(product2);
            
            Product product3 = new Product();
            product3.setName("智能门锁");
            product3.setDescription("指纹识别智能门锁，支持远程开门");
            product3.setPrice(899.99);
            product3.setStock(50);
            productRepository.save(product3);
            
            Product product4 = new Product();
            product4.setName("温湿度传感器");
            product4.setDescription("WiFi温湿度传感器，实时监测室内环境");
            product4.setPrice(79.99);
            product4.setStock(150);
            productRepository.save(product4);
            
            Product product5 = new Product();
            product5.setName("智能摄像头");
            product5.setDescription("1080P高清智能摄像头，支持移动侦测和夜视");
            product5.setPrice(199.99);
            product5.setStock(80);
            productRepository.save(product5);
        }
    }
}